﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Products
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class ProductLanguageDAO
    {

        #region general

        private static ProductLanguage LoadObj(IDataReader datareader)
        {
            ProductLanguage obj = new ProductLanguage();
            obj.PLID = MpDataHelp.GetInt(datareader, "PLID");
            obj.ProductID = MpDataHelp.GetInt(datareader, "ProductID");
            obj.LanguageCode = MpDataHelp.GetString(datareader, "LanguageCode");
            obj.ProductName = MpDataHelp.GetString(datareader, "ProductName");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Content = MpDataHelp.GetString(datareader, "Content");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            return obj;
        }

        #endregion

        public static int ProductLanguage_Add(ProductLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductLanguage_Add");
            db.AddOutParameter(dbCommand, "PLID", DbType.Int32, obj.PLID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, obj.ProductName);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@PLID"));
            return -1;
        }
        public static int ProductLanguage_Update(ProductLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductLanguage_Update");
            db.AddInParameter(dbCommand, "PLID", DbType.Int32, obj.PLID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, obj.ProductName);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int ProductLanguage_Delete(int PLID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductLanguage_Delete");
            db.AddInParameter(dbCommand, "PLID", DbType.Int32, PLID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<ProductLanguage> ProductLanguage_Select(int PLID, int ProductID, string LanguageCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_ProductLanguage_Select");
            db.AddInParameter(dbCommand, "PLID", DbType.Int32, PLID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, LanguageCode);
            List<ProductLanguage> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<ProductLanguage>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
